package hut.ai.team.table;

import hut.ai.team.object.QuanCo;
import hut.ai.team.screen.MainScreen;

import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class BanCo extends Group {

	public OCo ViTri[][] = new OCo[10][9];

	public BanCo() // Constructor khoi tao mang ViTri trong
	{
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				ViTri[i][j] = new OCo();
				ViTri[i][j].Trong = true;
				ViTri[i][j].Phe = 0;
				ViTri[i][j].TrangThai = 0;
				ViTri[i][j].Loai = 0;
				ViTri[i][j].imgMove.setPosition(j * 50 + 25, i * 53 + 112);
				ViTri[i][j].imgMove.setVisible(false);

				setClickCanMove(i, j);
				this.addActor(ViTri[i][j].imgMove);
			}
		}
	}

	public void ResetCanMove() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				ViTri[i][j].imgMove.setVisible(false);
			}
		}
	}

	public void OCoTrong(int i, int j) {
		ViTri[i][j].Trong = true;
		ViTri[i][j].Phe = 0;
		ViTri[i][j].TrangThai = 0;
		ViTri[i][j].Loai = 0;
	}
	
	public void DatQuanCo(QuanCo c,int i,int j){
		ViTri[i][j].Trong = false;
		ViTri[i][j].Phe = c.Phe;
		ViTri[i][j].TrangThai = c.TrangThai;
		if(c.TrangThai == 1)
			ViTri[i][j].Loai = c.LoaiCL;
		else
			ViTri[i][j].Loai = c.Loai;
	}

	public void ResetBanCo() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				ViTri[i][j].Trong = true;
				ViTri[i][j].Phe = 0;
				ViTri[i][j].TrangThai = 0;
				ViTri[i][j].Loai = 0;
				ViTri[i][j].imgMove.setVisible(false);
				setClickCanMove(i, j);
			}
		}
	}

	public void setClickCanMove(final int i, final int j) {
		ViTri[i][j].imgMove.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				System.out.print("click move: [" + i + "," + j + "," + ViTri[i][j].Phe + "," + ViTri[i][j].TrangThai + "," + ViTri[i][j].Loai + "," + ViTri[i][j].Trong + "]\t");
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong) {
					// Ghi thong tin vao gamelog
					// VanCo.LuuVaoGameLog(sender, VanCo.DanhDau);
					// Tra lai o co trong
					MainScreen.getInstance().mBanCo.OCoTrong(VanCo.DanhDau.Hang,VanCo.DanhDau.Cot);
					// Dat quan co vao vi tri [i,j] tren ban co
					VanCo.DatQuanCo(VanCo.DanhDau, i, j);
					VanCo.ply++;
					// Thay doi luot di
					VanCo.DoiLuotDi();
					VanCo.KiemTraChieuBi();
					if (VanCo.winner != 2)
					{
						if(MainScreen.getInstance().mode == 2)
						{
							if (VanCo.winner == 0) MainScreen.getInstance().dialog.show("Player red has winner");
							if (VanCo.winner == 1) MainScreen.getInstance().dialog.show("Player black has winner");
							MainScreen.getInstance().dialog.setVisible(true);
						}else
						{
							if (VanCo.winner == 0) MainScreen.getInstance().dialog.show("Computer has winner");
							if (VanCo.winner == 1) MainScreen.getInstance().dialog.show("Player 1 has winner");
							MainScreen.getInstance().dialog.setVisible(true);
						}
						KhoaBanCo();
					}
					// Kiem tra chieu tuong
					VanCo.KiemTraChieuTuong();
				} else {
					// an quan
					int phekia = 0;
					if (VanCo.DanhDau.Phe == 1)
						phekia = 2;
					else
						phekia = 1;
					QuanCo temp_c = new QuanCo();
					if (MainScreen.getInstance().mode == 2) {
						if (MainScreen.getInstance().n1.Phe == phekia) {
							for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
								if (i == MainScreen.getInstance().n1.dsQuanCo.get(k).Hang && j == MainScreen.getInstance().n1.dsQuanCo.get(k).Cot)
									temp_c = MainScreen.getInstance().n1.dsQuanCo.get(k);
							}
						} else {
							for (int k = 0; k < MainScreen.getInstance().n2.dsQuanCo.size(); k++) {
								if (i == MainScreen.getInstance().n2.dsQuanCo.get(k).Hang && j == MainScreen.getInstance().n2.dsQuanCo.get(k).Cot)
									temp_c = MainScreen.getInstance().n2.dsQuanCo.get(k);
							}
						}
					} else {
						if (MainScreen.getInstance().n1.Phe == phekia) {
							for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
								if (i == MainScreen.getInstance().n1.dsQuanCo.get(k).Hang && j == MainScreen.getInstance().n1.dsQuanCo.get(k).Cot)
									temp_c = MainScreen.getInstance().n1.dsQuanCo.get(k);
							}
						} else {
							for (int k = 0; k < MainScreen.getInstance().computer.dsQuanCo.size(); k++) {
								if (i == MainScreen.getInstance().computer.dsQuanCo.get(k).Hang && j == MainScreen.getInstance().computer.dsQuanCo.get(k).Cot)
									temp_c = MainScreen.getInstance().computer.dsQuanCo.get(k);
							}
						}
					}
					// Ghi thong tin nuoc di vao gamelog
					// VanCo.LuuVaoGameLog(sender, temp_c);
					// An quan co cua doi phuong
					VanCo.AnQuanCo(temp_c);
					// Tra lai o co trong
					MainScreen.getInstance().mBanCo.OCoTrong(VanCo.DanhDau.Hang, VanCo.DanhDau.Cot);
					// Thiet lap quan co vao vi tri tren ban co
					VanCo.DatQuanCo(VanCo.DanhDau, i, j);
					VanCo.ply++;
					// Thay doi luot di
					System.out.println("Luot di trong BanCo1: " +VanCo.LuotDi);
					VanCo.DoiLuotDi();
					System.out.println("Luot di trong BanCo2: " +VanCo.LuotDi);
					VanCo.KiemTraChieuBi();
					System.out.println("Winner: "+VanCo.winner);
					if (VanCo.winner != 2)
					{
						if(MainScreen.getInstance().mode == 2)
						{
							if (VanCo.winner == 0) MainScreen.getInstance().dialog.show("Player black has winner");
							if (VanCo.winner == 1) MainScreen.getInstance().dialog.show("Player red has winner");
							MainScreen.getInstance().dialog.setVisible(true);
						}else
						{
							if (VanCo.winner == 0) MainScreen.getInstance().dialog.show("Computer has winner");
							if (VanCo.winner == 1) MainScreen.getInstance().dialog.show("Player 1 has winner");
							MainScreen.getInstance().dialog.setVisible(true);
						}
						KhoaBanCo();
					}
					// Kiem tra chieu tuong
					VanCo.KiemTraChieuTuong();
				}
				MainScreen.getInstance().mBanCo.ResetCanMove();
				addAction(Actions.sequence(Actions.delay(1.5f), new Action() {
					
					@Override
					public boolean act(float delta) {
						if(MainScreen.getInstance().mode == 1){
							synchronized (MainScreen.getInstance().computer) {
								if(MainScreen.getInstance().mode == 1){
									if(VanCo.LuotDi == MainScreen.getInstance().computer.Phe){
										MainScreen.getInstance().computer.ComputerThink();
									}
								}
							}
						}
						return true;
					}
				}));
		
//				System.out.println();
//				System.out.println("Sau khi move");
//				showData();
			}
		});
	}

	public void KhoaBanCo() {
		VanCo.Finish = true;
		if (MainScreen.getInstance().mode == 2) {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				MainScreen.getInstance().n1.dsQuanCo.get(i).Khoa = true;
			}
			for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
				MainScreen.getInstance().n2.dsQuanCo.get(i).Khoa = true;
			}
		} else {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				MainScreen.getInstance().n1.dsQuanCo.get(i).Khoa = true;
			}
			for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
				MainScreen.getInstance().computer.dsQuanCo.get(i).Khoa = true;
			}
		}
	}

	public void MoBanCo() {
		if (VanCo.LuotDi == 0) {
			VanCo.LuotDi = 1;
			VanCo.DoiLuotDi();
		} else {
			VanCo.LuotDi = 0;
			VanCo.DoiLuotDi();
		}
	}

	public void showData() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				System.out.print("[" + i + "," + j + "," + ViTri[i][j].Phe
						+ "," + ViTri[i][j].TrangThai + "," + ViTri[i][j].Loai
						+ "]\t");
			}
			System.out.println();
		}
	}
}
